package com.bosssoft.hr.jobmonitor.core.auth;

import com.bosssoft.hr.jobmonitor.controller.UserController;
import com.bosssoft.hr.jobmonitor.entity.Auth;
import com.bosssoft.hr.jobmonitor.util.LoggerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/***
 * @Author lys(cherish)
 * @Description 权限认证拦截器
 * @Date  2019/2/26
 **/
public class AuthInterceptor implements HandlerInterceptor {

    private static final Logger logger= LoggerFactory.getLogger(AuthInterceptor.class);

    /**bosss
     * @param request 请求
     * @param response 响应
     * @param handler 处理器
     * @return 是否通过拦截器的标志
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Auth auth = (Auth)session.getAttribute("auth");
        if(auth==null){
            logger.info(LoggerUtil.fmtLog("未登入，禁止访问"));
            response.sendRedirect(request.getContextPath()+"/");
        }
       return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
